Like any kind of apps, JavaScript apps also have to be written well.
Otherwise, we run into all kinds of issues later on.
In this article, we’ll look at some tips we should follow to write JavaScript code faster and better.
Convert Strings to Uppercase
We can call toUpperCase
to change the string characters to uppercase.
For instance, we can write:
'foo'.toUpperCase()
then it returns 'FOO'
.
Convert Strings to Lowercase
To convert a string to lowercase, we can use the toLowerCase
method.
For instance, we can write:
'FOO'.`toLowerCase`()
then it returns 'foo'
.
Converting Strings to Uppercase in a Locale-Sensitive Way
We can use the toLocaleUpperCase
method to convert a string into a locale-sensitive upper case string.
For instance, we can write:
'foo'.toLocaleUpperCase('tr');
Then we get “FOO”
.
Converting Strings to Lowercase in a Locale-Sensitive Way
There’s the toLocaleLowerCase
method to convert a string into a locale-sensitive lowercase string.
For instance, we can write:
'FOO'.toLocaleLowerCase('tr');
Then we get “foo”
.
Extracting a Substring From a String
To extract a substring from a string, we can use the substring
method.
It takes a start and ending index of a string.
For instance, we can write:
'jane, joe, and alex'.substring(5, 10);
and that returns “ joe,”
.
The index can also be negative.
So we can write:
'jane, joe, and alex'.substring(-10, 4);
and get 'jane'
.
Negative indexes start from -1 which is the index of the last character, -2 is the index of the 2n last character, and so on.
Check if a String Starts With Something
Strings have a startsWith
method that we can use to check if a string starts with a given substring.
For instance:
'jane, joe, and alex'.`startsWith`('jane');
returns true
.
On the other hand;
'jane, joe, and alex'.`startsWith`('foo');
returns false
.
It also takes a second argument to make startsWith
starts the search from a given index.
For instance, we can write:
'jane, joe, and alex'.`startsWith`('jane', 5);
Then startsWith
starts searching with index 5.
Split a String According to a Separator
We can call split
according to a separator.
For instance, we can write:
'jane, joe, and alex'.`split`(' ');
Then we split the string according to the space characters.
So we get [“jane,”, “joe,”, “and”, “alex”]
as the result.
Extract a Slice of a String
The slice
method can help with extracting a substring from a string.
It takes a start and end index.
For instance, we can write the following code:
'jane, joe, and alex'.`slice`(5, 10)
Then that returns the string “ joe,”
.
It also takes negative indexes, with -1 being the last character, -2 being the 2nd last, and so on.
For instance, we can write:
'jane, joe, and alex'.`slice`(-10, -5)
and it returns “, and”
.
Make a String Repeat
The repeat
method can make a string repeat.
For instance, we can write:
'fooo'.repeat(3)
Then we get “fooofooofooo”
.
Adding Characters to the Beginning of the String
The padStart
method takes an argument for the target length of the string after padding.
The 2nd argument is the string to pad our string with.
For instance, we can write:
‘foo’.padStart(8, ‘abcd’)
Then it returns “abcdafoo”
since it pads 'foo'
with 'abcd'
repeated until the returned string has 8 characters.
Adding Characters to the End of the String
The padEnd
method takes an argument for the target length of the string after padding to the end.
The 2nd argument is the string to pad our string with.
For instance, we can write:
‘foo’.`padEnd`(8, ‘abcd’)
Then we return “fooabcda”
since we add the string to the end of the string.
Removing Extra Unicode Characters
We can use normalize
to remove extra characters by replacing them with single-character equivalents.
For instance, we can write:
'\u1E9B\u0323'.normalize()
Then we get “ẛ̣”
.
Find the String Matches a Given Regex Pattern
The match
method returns the matches of a given regex pattern.
For instance, we can write:
'foo'.match(/(foo|bar)/)
Then that would return [ "foo", "foo" ]
.
Compare Strings by Using the Locale Compare
localeCompare
compares the strings in a locale-sensitive way.
For instance, we can write:
'a'.localeCompare('à', 'fr-ca')
Then that returns -1 since ‘à’
is behind 'a'
.
Conclusion
There are string methods that we can do many things with.
We can compare strings and pad them to a given length.